#include "Sha256.h"
#include <string.h>
#include "openssl/sha.h"
#include "openssl/crypto.h"
#include "../EnclaveWrap/EnclaveWrap.h"

Sha256::Sha256()
{
}


Sha256::~Sha256()
{
}

int Sha256::encrypt(const char * in_buf, int in_buf_len, char * out_buf, int out_buf_size)
{
    if (out_buf_size < SHA256_DIGEST_LENGTH)
    {
        log_error("out_buf %d < %d", out_buf_size, SHA256_DIGEST_LENGTH);
        return 0;
    }

    SHA256_CTX c;
    unsigned char md[SHA256_DIGEST_LENGTH];
    SHA256((unsigned char *)in_buf, in_buf_len, md);

    SHA256_Init(&c);
    SHA256_Update(&c, in_buf, in_buf_len);
    SHA256_Final(md, &c);
    OPENSSL_cleanse(&c, sizeof(c));

    memcpy(out_buf, md, SHA256_DIGEST_LENGTH);

    return SHA256_DIGEST_LENGTH;
}

int Sha256::decrypt(const char * in_buf, int in_buf_len, char * out_buf, int out_buf_size)
{
    return 0;
}

int Sha256::encrypt_size(int buf_len)
{
    return SHA256_DIGEST_LENGTH;
}

int Sha256::decrypt_size(int buf_len)
{
    return 0;
}
